<?php
	add_action('widgets_init', 'register_subscribe_wg');
	function register_subscribe_wg()
	{
		register_widget('subscribe');
	}

	class subscribe extends WP_Widget // widget class
	{
		function subscribe()// widget setting | class name must be same function name
		{ 
			$widget_ops = array('classname' => 'subscribe-w', 'description' => 'Display subscribe widget (Body head / Body foot)');
			$control_ops = array(
				'width' => 250,
				'height' => 350,
				'id_base' => 'subscribe');
			$this->WP_Widget('subscribe', 'DealX - Subscribe', $widget_ops, $control_ops);
		}
    
	function widget($args, $instance)// display widget
	{
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
        $plhd = $instance['plhd'];
        $btn = $instance['btn'];
        $display_at = $instance['display_at'];
        
        if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['btn_subscribe'] ))
        {
            if($_SERVER["HTTP_X_FORWARDED_FOR"])
                $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
            else
                $ip_address = $_SERVER['REMOTE_ADDR'];
            
            if($_POST['sub_email_head'])
            {
                $email = $_POST['sub_email_head'];
                if(is_email($email))
                {
                    $insert = add_subscribe_email($email, $ip_address);
                    if($insert > 0)
                        $suc = 'Thanks for subscribe us';
                }
            }
            else if($_POST['sub_email_foot'])
            {
                $email = $_POST['sub_email_foot'];
                if(is_email($email))
                {
                    $insert = add_subscribe_email($email, $ip_address);
                    if($insert > 0)
                        $suc = 'Thanks for subscribe us';
                }
            }
        }
        if($display_at == 'Body Head'):
    ?>
        <!-- body head -->
        <div class="row-fluid subscribe">
            <div class="subscribe-text">
                <?php echo $title; ?>
            </div>
            <div class="subscribe-form">
                <form method="post" action="<?php echo cpx_current_url(); ?>" id="subscribe_head">
                    <input type="text" name="sub_email_head" placeholder="<?php echo ($suc?$suc:$plhd); ?>"/>
                    <input type="submit" name="btn_subscribe" class="text-transform" value="<?php echo $btn; ?>"/>
                </form>
            </div>
        </div>
    <?php
        elseif($display_at == 'Body Foot'):
    ?> 
        <!-- body foot -->
        <div class="row-fluid subscribe">
            <div class="subscribe-text">
                <?php echo $title; ?>
            </div>
            <div class="subscribe-form">
                <form method="post" action="<?php echo cpx_current_url(); ?>" id="subscribe_foot">
                    <input type="text" name="sub_email_foot" placeholder="<?php echo ($suc?$suc:$plhd); ?>"/>
                    <input type="submit" name="btn_subscribe" class="text-transform" value="<?php echo $btn; ?>"/>
                </form>
            </div>
        </div>
    <?php  
        endif;
	}
	function update($new_instance, $old_instance)// update widget
	{
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['plhd'] = $new_instance['plhd'];
        $instance['btn'] = $new_instance['btn'];
        $instance['display_at'] = $new_instance['display_at'];
        return $instance;
	}

	function form($instance)
	{
	   $arr_display_at = array(
       'body_head' => 'Body Head',
       'body_foot' => 'Body Foot'
       );
	   ?>
         <div style="color: #333;">
    		<p>        
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<textarea placeholder="ex:We offer amazing and exclusive deals for web professionals. Sign up for free to get deals:" style="width: 100%;height: 100px;" name="<?php echo $this->get_field_name('title');?>" id="<?php echo $this->get_field_id('title'); ?>"><?php echo $instance['title']; ?></textarea>
    		</p>
            <p>        
    			<label for="<?php echo $this->get_field_id('plhd');?>"><?php echo 'Placeholder text'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('plhd'); ?>" name="<?php	echo $this->get_field_name('plhd');?>" value="<?php echo $instance['plhd'];?>" style="width:100%;" placeholder="ex: Enter your email address" />
    		</p>
            <p>        
    			<label for="<?php echo $this->get_field_id('btn');?>"><?php echo 'Button text'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('btn'); ?>" name="<?php	echo $this->get_field_name('btn');?>" value="<?php echo $instance['btn'];?>" style="width:100%;" placeholder="ex: Subscribe me" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('display_at');?>"><?php echo 'Display at';?>
    			<select name="<?php echo $this->get_field_name('display_at');?>" id="<?php echo $this-> get_field_id('display_at')?>" style="width: 60%;">
                <?php  
                        foreach ($arr_display_at as $v) {
                            if($instance['display_at'] == $v)
                                $selected = 'selected';
                            else
                                $selected = '';
                            echo "<option value='{$v}' {$selected}>$v</option>";
                        }
                ?>
                </select>
    		</p>
        </div>
    <?php
	}
}
?>
